package ru.ifmo.swing.genetics.selection;

import org.uncommons.watchmaker.framework.EvaluatedCandidate;
import org.uncommons.watchmaker.framework.SelectionStrategy;
import ru.ifmo.swing.EvolutionControlWithConfigProvider;

import javax.swing.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author avhaliullin
 */
public abstract class AbstractDelegationSelectionControl implements EvolutionControlWithConfigProvider, SelectionStrategy<Object> {
    private SelectionStrategyChooser<Object> selectionStrategy;
    private SelectionStrategy<Object> strategy;

    public AbstractDelegationSelectionControl(Map<String, SelectionStrategy<? super Object>> options) {
        selectionStrategy = new SelectionStrategyChooser<Object>(options);
        strategy = getStrategy(selectionStrategy);
    }

    public JComponent getControl() {
        return selectionStrategy.getControl();
    }

    public void reset() {
        selectionStrategy.reset();
    }

    public void setDescription(String description) {
        selectionStrategy.setDescription(description);
    }

    public <S> List<S> select(List<EvaluatedCandidate<S>> population, boolean naturalFitnessScores, int selectionSize, Random rng) {
        return strategy.select(population, naturalFitnessScores, selectionSize, rng);
    }

    public Map<String, Object> getParamValues() {
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("Delegate strategy", selectionStrategy);
        return res;
    }

    protected abstract SelectionStrategy<Object> getStrategy(SelectionStrategy<Object> delegate);
}
